import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { fileURLToPath } from 'url'
import { viteCommonjs } from '@originjs/vite-plugin-commonjs'
import WindiCSS from 'vite-plugin-windicss'
const __filename = fileURLToPath(import.meta.url)
const __dirname = path.dirname(__filename)
const resolve = dir => {
  return path.resolve(__dirname, dir)
}

export default defineConfig(({ command }) => {
  return {
    base: './',
    plugins: [
      vue({
        script: {
          defineModel: true
        }
      }),
      WindiCSS(),
      viteCommonjs({
        skipPreBuild: command === 'serve'
      })
    ],
    envDir: './config',
    resolve: {
      // 设置别名
      alias: {
        '@': resolve('src'),
        assets: resolve('src/assets'),
        components: resolve('src/components'),
        stores: resolve('src/stores'),
        api: resolve('src/api')
      }
    },
    server: {
      port: '8080',
      // 是否自动在浏览器打开
      open: false,
      // 配置代理
      proxy: {
        '/api': {
          // target: 'https://2867webg2383.vicp.fun',
          target: 'http://116.62.223.201:9999',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    build: {
      outDir: 'dist',
      emptyOutDir: true,
      commonjsOptions: {
        transformMixedEsModules: true
      },
      chunkSizeWarningLimit: 1000,
      rollupOptions: {
        // treeshake: true,
        output: {
          assetFileNames: 'assets/[name]-[hash][extname]',
          chunkFileNames: 'js/[name]-[hash].js',
          entryFileNames: 'js/[name]-[hash].js', //入口文件输出的文件夹名称
          compact: true //压缩代码，删除换行符等
        }
      }
    }
  }
})
